extends MeshInstance


# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
	var height = Image.new()
	height.load("res://resources/planets/planet/height.png")
	height.lock()
	var max_x = 100
	var max_y = 100
	var amplifier = 10
	var surf = SurfaceTool.new()
	surf.begin(Mesh.PRIMITIVE_TRIANGLES)
	for y in range(max_y):
		for x in range(max_x):
			if max_x >= x + 1 and max_y >= y + 1:
				var point1 = Vector3(
					x,
					height.get_pixel(x, y).v * amplifier,
					y
				)
				var point2 = Vector3(
					x + 1,
					height.get_pixel(x + 1, y).v * amplifier,
					y
				)
				var point3 = Vector3(
					x,
					height.get_pixel(x , y + 1).v * amplifier,
					y + 1
				)
				var point4 = Vector3(
					x + 1,
					height.get_pixel(x + 1, y + 1).v * amplifier,
					y + 1
				)
				print(point1)
				print(point2)
				print(point3)
				print(point4)
				
				# First triangle
				surf.add_vertex(point1)
				surf.add_vertex(point2)
				surf.add_vertex(point3)
				# Second triangle
				surf.add_vertex(point4)
				surf.add_vertex(point3)
				surf.add_vertex(point2)
	surf.generate_normals()
	var mat = SpatialMaterial.new()
	mat.flags_unshaded = false
	mat.albedo_color = Color.brown
	
	self.mesh = surf.commit()
	self.material_override = mat
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass
